package com.example.emptytest.TabFragment;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.example.emptytest.Adapter.memberListAdapter;
import com.example.emptytest.R;
import com.example.emptytest.member;

import java.util.ArrayList;
import java.util.List;

public class TabFragment1 extends Fragment {
    private String mTitle;
    private String[][] data_list1_2 = {{"陈萌","20181713","大数据与软件学院"}
            , {"陈萌","20181713","大数据与软件学院"}
            , {"陈萌","20181713","大数据与软件学院"}
            , {"陈萌","20181713","大数据与软件学院"}
            , {"陈萌","20181713","大数据与软件学院"}
            , {"陈萌","20181713","大数据与软件学院"}
            , {"陈萌","20181713","大数据与软件学院"}};
    private List<member> memberLists = new ArrayList<member>();
    private ListView list1_2;
    private boolean list1_2_load = false;
    // 动态卡片加载
    private LinearLayout llContentView;

    //这个构造方法是便于各导航同时调用一个fragment
    public TabFragment1(String title){
        mTitle=title;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        View view;
        if(mTitle.compareTo("社团信息") == 0)
            view=inflater.inflate(R.layout.fragment_tab1_1,container,false); //这里筛选 切换界面
        else if(mTitle.compareTo("成员列表") == 0) {
            view = inflater.inflate(R.layout.fragment_tab1_2, container, false); //这里筛选 切换界面
            if(list1_2_load == false) {

                initMemberList();
                list1_2_load = true;
            }
            memberListAdapter adapter1_2 = new memberListAdapter(getActivity(), R.layout.memberlist_item, memberLists);
            list1_2 = view.findViewById(R.id.club_member_list);
            list1_2.setAdapter(adapter1_2);
            list1_2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                    member meMber = memberLists.get(position);
                    Toast.makeText(getActivity(), meMber.getName(), Toast.LENGTH_SHORT).show();
                }
            });
        }
        else if(mTitle.compareTo("社团活动") == 0) {
            view = inflater.inflate(R.layout.fragment_tab1_3, container, false); //这里筛选 切换界面
            llContentView = view.findViewById(R.id.content_view3);
            String name = getContext().getResources().getString(R.string.club_act1_name);
            String content = getContext().getResources().getString(R.string.club_act1_content);
            String time = getContext().getResources().getString(R.string.club_act1_time);
            addCard(llContentView,view,name,content,time);

        }
        else if(mTitle.compareTo("活动审核") == 0) {
            view = inflater.inflate(R.layout.fragment_tab1_4, container, false); //这里筛选 切换界面
            llContentView = view.findViewById(R.id.content_view4);
            addCard(llContentView,view,"审核通过","无","2020-7-7");
        }
        else {
            view = inflater.inflate(R.layout.fragment_tab1_5, container, false); //这里筛选 切换界面
            llContentView = view.findViewById(R.id.content_view5);
            addCard(llContentView,view,"公告","无","2020-7-7");
        }
        System.out.println("初始化页面");



        return view;
    }

    private void addCard(LinearLayout LLcontentView,View v,String name,String content,String time) {
        if(v == null)
            return;
            // 开始添加卡片
            // card
            CardView cardView = new CardView(getActivity());
            LinearLayout.LayoutParams cardLayoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    dp(180));
            cardLayoutParams.setMargins(dp(10),dp(10),dp(10),dp(10));
            cardView.setLayoutParams(cardLayoutParams);
            cardView.setRadius(dp(8));
            cardView.setCardBackgroundColor(getContext().getResources().getColor(R.color.card_trans));
            // LinearLayout
            LinearLayout layout = new LinearLayout(getActivity());
            LinearLayout.LayoutParams LayoutParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            layout.setLayoutParams(LayoutParams);
            layout.setOrientation(LinearLayout.VERTICAL);
            // TextView
            TextView title = new TextView(getActivity());
            LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            title.setLayoutParams(titleParams);
            title.setGravity(Gravity.CENTER);
            title.setText(name);
            title.setTextSize(20);
            title.setBackground(getContext().getResources().getDrawable(R.drawable.menu_press2));

            TextView contentView = new TextView(getActivity());
            LinearLayout.LayoutParams contentParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            contentParams.setMargins(dp(10),dp(10),dp(10),dp(10));
            contentView.setLayoutParams(contentParams);
            contentView.setText(content);
            contentView.setTextSize(15);

            TextView timeView = new TextView(getActivity());
            LinearLayout.LayoutParams timeParams = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);

            timeParams.setMargins(0,dp(20),10,0);
            timeView.setLayoutParams(timeParams);
            timeView.setText(time);
            timeView.setGravity(Gravity.RIGHT);


            layout.addView(title);
            layout.addView(contentView);
            layout.addView(timeView);
            cardView.addView(layout);
            LLcontentView.addView(cardView);

    }
    // int dp 转换
    private int dp(int i) {
        return ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i, getResources().getDisplayMetrics()));
    }

    private void initMemberList() {
        System.out.println(data_list1_2.length);
            for(int i = 0;i < data_list1_2.length;i ++){
                member member0 = new member(data_list1_2[i][0],data_list1_2[i][1],data_list1_2[i][2]);
                memberLists.add(member0);
            }
    }
}
